package org.summer.lawyer.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.summer.common.core.utils.MapstructUtils;
import org.summer.common.mybatis.core.page.PageQuery;
import org.summer.common.mybatis.core.page.TableDataInfo;
import org.summer.lawyer.domain.UserWalletLog;
import org.summer.lawyer.domain.bo.userwalletlog.UserWalletLogBo;
import org.summer.lawyer.domain.vo.userwalletlog.UserWalletLogVo;
import org.summer.lawyer.mapper.UserWalletLogMapper;
import org.summer.lawyer.service.IUserWalletLogService;
import org.summer.lawyer.util.IdWorkerUtil;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 用户钱包流水记录Service业务层处理
 *
 * @author summer
 * @date 2024-06-25
 */
@RequiredArgsConstructor
@Service
public class UserWalletLogServiceImpl extends ServiceImpl<UserWalletLogMapper, UserWalletLog>  implements IUserWalletLogService {


    /**
     * 查询用户钱包流水记录
     *
     * @param id 主键
     * @return 用户钱包流水记录
     */
    @Override
    public UserWalletLogVo selectUserWalletLogVoById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询用户钱包流水记录列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 用户钱包流水记录分页列表
     */
    @Override
    public TableDataInfo<UserWalletLogVo> selectPageList(UserWalletLogBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<UserWalletLog> lqw = buildQueryWrapper(bo);
        Page<UserWalletLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的用户钱包流水记录列表
     *
     * @param bo 查询条件
     * @return 用户钱包流水记录列表
     */
    @Override
    public List<UserWalletLogVo> selectUserWalletLogVoList(UserWalletLogBo bo) {
        LambdaQueryWrapper<UserWalletLog> lqw = Wrappers.lambdaQuery();
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<UserWalletLog> buildQueryWrapper(UserWalletLogBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<UserWalletLog> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getWxUserId() != null, UserWalletLog::getWxUserId, bo.getWxUserId());
        lqw.eq(StrUtil.isNotBlank(bo.getWalletNo()), UserWalletLog::getWalletNo, bo.getWalletNo());
        lqw.eq(bo.getTargetType() != null, UserWalletLog::getTargetType, bo.getTargetType());
        lqw.eq(StrUtil.isNotBlank(bo.getTargetUuid()), UserWalletLog::getTargetUuid, bo.getTargetUuid());
        lqw.eq(bo.getActionType() != null, UserWalletLog::getActionType, bo.getActionType());
        lqw.eq(bo.getPreAmount() != null, UserWalletLog::getPreAmount, bo.getPreAmount());
        lqw.eq(bo.getAmount() != null, UserWalletLog::getAmount, bo.getAmount());
        lqw.eq(bo.getPostAmount() != null, UserWalletLog::getPostAmount, bo.getPostAmount());
        return lqw;
    }

    /**
     * 新增用户钱包流水记录
     *
     * @param bo 用户钱包流水记录
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(UserWalletLogBo bo) {
        UserWalletLog add = MapstructUtils.convert(bo, UserWalletLog.class);
        validEntityBeforeSave(add);
        add.setCreateTime(DateUtil.date());
        add.setWalletNo(IdWorkerUtil.nextIdStr());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改用户钱包流水记录
     *
     * @param bo 用户钱包流水记录
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(UserWalletLogBo bo) {
        UserWalletLog update = MapstructUtils.convert(bo, UserWalletLog.class);
        validEntityBeforeSave(update);
        update.setCreateTime(DateUtil.date());
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(UserWalletLog entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除用户钱包流水记录信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
